package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.apis.article.ApArticleClient;
import com.heima.common.constants.ArticleConstants;
import com.heima.common.redis.CacheService;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.UserRelationDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserFan;
import com.heima.model.user.pojos.ApUserFollow;
import com.heima.user.mapper.ApUserFanMapper;
import com.heima.user.mapper.ApUserFollowMapper;
import com.heima.user.service.ApUserRelationService;
import com.heima.utils.thread.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class ApUserRelationServiceImpl implements ApUserRelationService {

    @Autowired
    private ApUserFollowMapper apUserFollowMapper;

    @Autowired
    private ApUserFanMapper apUserFanMapper;

    @Autowired
    private ApArticleClient apArticleClient;

    @Autowired
    private CacheService cacheService;


    /**
     * 关注与取消
     * @param userRelationDto
     * @return
     */
    @Override
    public ResponseResult userFollow(UserRelationDto userRelationDto) {
        //参数校验
        if (userRelationDto==null||userRelationDto.getAuthorId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApUser apUser = AppThreadLocalUtils.getUser();
        if (userRelationDto.getOperation()==0){
            ApUserFollow apUserFollow=new ApUserFollow();
            //ApUser apUser = AppThreadLocalUtils.getUser();
            Long articleId = userRelationDto.getArticleId();
            ApArticle apArticle = apArticleClient.getById(articleId);
            apUserFollow.setUserId(apUser.getId());
            apUserFollow.setFollowId(userRelationDto.getAuthorId());
            apUserFollow.setCreatedTime(new Date());
            apUserFollow.setFollowName(apArticle.getAuthorName());
            apUserFollow.setLevel((short)0);
            apUserFollow.setIsNotice((short)0);
            //关注
            apUserFollowMapper.insert(apUserFollow);

            ApUserFan apUserFan=new ApUserFan();
            apUserFan.setFansId(apUser.getId());
            apUserFan.setFanName(apUser.getName());
            apUserFan.setCreatedTime(new Date());
            apUserFan.setIsDisplay((short)0);
            apUserFan.setIsShieldLetter((short)0);
            apUserFan.setIsShieldComment((short)0);
            apUserFan.setLevel((short)0);
            apUserFan.setUserId(userRelationDto.getAuthorId());
            apUserFanMapper.insert(apUserFan);

            //存入redis
            cacheService.hPut(ArticleConstants.USER_RELATION_FOLLOW,apUser.getId()+"::"+userRelationDto.getAuthorId(),"0");

        }else {
            //取消关注
            apUserFollowMapper.delete(Wrappers.<ApUserFollow>lambdaQuery().
                    eq(ApUserFollow::getFollowId,userRelationDto.getAuthorId()));
            apUserFanMapper.delete(Wrappers.<ApUserFan>lambdaQuery().
                    eq(ApUserFan::getFansId,apUser.getId()));
            cacheService.hDelete(ArticleConstants.USER_RELATION_FOLLOW,apUser.getId()+"::"+userRelationDto.getAuthorId());
        }
        return ResponseResult.okResult("");
    }
}
